package control;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import persistence.FuncionarioDao;
import entity.Funcionario;


@WebServlet("/Controle")
public class Controle extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String cmd = request.getParameter("cmd");
		if(cmd.equalsIgnoreCase("gravar")){
			gravar(request, response);
		}
		
	}
	
	protected void gravar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Funcionario f = new Funcionario();
		FuncionarioDao fdao = new FuncionarioDao();
		
		try {
			f.setNome(request.getParameter("nome"));
			f.setSalario(new Double(request.getParameter("salario")));
			
			fdao.create(f);
			
			request.setAttribute("msg", "Funcionario Cadastrado");
			
			
		} catch (Exception ex) {
			request.setAttribute("msg", "Error: " + ex.getMessage());
			ex.printStackTrace();
		}finally{
			request.getRequestDispatcher("cadastro_funcionario.jsp").forward(request, response);
		}
		
	}

}






